/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.sessionbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.entities.RegisteredUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Breath
 */
@Stateless
public class ArticleFacade extends AbstractFacade<Article> implements ArticleFacadeLocal {

    @PersistenceContext(unitName = "com.blog.city_CheckTheDep_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    /**
     * 
     * @return
     */
    protected EntityManager getEntityManager() {
        return em;
    }

    /**
     * 
     */
    public ArticleFacade() {
        super(Article.class);
    }

    /**
     * 
     * @return
     */
    @Override
    public Article latestArt() {
        return (Article) getEntityManager().createQuery("SELECT a FROM Article a ORDER BY id DESC").setMaxResults(1).getSingleResult();
    }

    /**
     * 
     * @param regId
     * @return
     */
    @Override
    public List<Article> findByRegUserId(RegisteredUser regId) {
        return (List<Article>) this.getEntityManager().createQuery("SELECT a FROM Article a WHERE a.registeredUserId= :registeredUserId").setParameter("registeredUserId", regId).getResultList();
    }
}
